package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.MvpActivity;
import com.zhengtoudai.ui.adapter.MyRedpacketAdapter;
import com.zhengtoudai.ui.bean.MyRedpacketModel;
import com.zhengtoudai.ui.presenter.MyRedpacketPresenter;
import com.zhengtoudai.ui.view.MyRedpacketView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhengtoudai.R.id.refresh;

public class MyRedpacketActivity extends MvpActivity<MyRedpacketPresenter> implements MyRedpacketView
        , OnRefreshListener
        , OnLoadMoreListener
        ,AppBarLayout.OnOffsetChangedListener{

    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(refresh)
    SwipeToLoadLayout mRefresh;
    @Bind(R.id.appbar)
    AppBarLayout mAppbar;
    private MyRedpacketAdapter mAdapter;
    private int pageNum;
    private int totalNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_redpacket);
        ButterKnife.bind(this);
    }

    @Override
    protected MyRedpacketPresenter createPresenter() {
        return new MyRedpacketPresenter(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("我的红包");
    }

    @Override
    protected void initView() {
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mSwipeTarget.setLayoutManager(new LinearLayoutManager(context));
        mAdapter = new MyRedpacketAdapter();
        mSwipeTarget.setAdapter(mAdapter);
        mRefresh.post(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(true);
            }
        });
        autoLoading(mRefresh, mSwipeTarget);
        mAppbar.addOnOffsetChangedListener(this);

    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset==0){
            mRefresh.setRefreshEnabled(true);
        }else{
            mRefresh.setRefreshEnabled(false);
        }
    }
    @Override
    public void onLoadMore() {
        if (pageNum < totalNum) {
            int current = pageNum + 1;
            mvpPresenter.loadData(context,"10", current + "");
        } else {
            mRefresh.setLoadingMore(false);
            AbToastUtil.showToast(context, "已无更多数据");
        }
    }

    @Override
    public void onRefresh() {
        mAdapter.clearData();
        mvpPresenter.loadData(context,"10", "1");
    }

    @Override
    public void getDataSuccess(MyRedpacketModel model) {
        AbToastUtil.showToast(context, model.getMessage());
        MyRedpacketModel.MyRedpacketBean mInfo = model.getResult();
        totalNum = Integer.valueOf(mInfo.getTotalPage());
        pageNum = Integer.valueOf(mInfo.getPageNum());
        if (mInfo.getRedList() == null || mInfo.getRedList().size() == 0) {

            AbToastUtil.showToast(context,"暂无数据");
        } else {

            mAdapter.addData(mInfo.getRedList());
        }
    }

    @Override
    public void getDataFail(int code) {
        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        } else {
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void showLoading() {
        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        } else {
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void hideLoading() {

    }

}
